package com.share.lottery.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.share.lottery.R;
import com.share.lottery.model.AdvertBean;
import com.share.uitool.base.Log;
import com.share.uitool.view.urlimage.UrlImageViewCallback;
import com.share.uitool.view.urlimage.UrlImageViewHelper;

/**
 * 首页Gallery �?adapter
 * 
 * @author lmj
 * 
 */
@SuppressWarnings("deprecation")
public class GalleryAdapter extends BaseAdapter {
	List<AdvertBean> imageLists;
	Context mContext;

	public GalleryAdapter(Context context, List<AdvertBean> data) {
		mContext = context;
		imageLists = data;

		if (data != null) {
			Log.e("data----------" + imageLists.toString());
		}
	}

	public void clearData() {
		imageLists.clear();
	}

	public void setDatas(List<AdvertBean> data) {
		if (data == null) {
			return;
		}

		if (imageLists == null) {
			imageLists = data;
		} else {
			imageLists.addAll(data);
		}

		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (imageLists != null) {
			return imageLists.size();
		}
		return 0;
	}

	@Override
	public AdvertBean getItem(int position) {

		if (imageLists != null) {
			return imageLists.get(position);
		}

		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ImageView iv;

		if (convertView != null) {
			iv = (ImageView) convertView;
		} else {
			iv = new ImageView(mContext);
			iv.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			iv.setScaleType(ScaleType.FIT_XY);
			convertView = iv;
		}

		final AdvertBean bean = imageLists.get(position);


		UrlImageViewHelper.setUrlDrawable(iv, bean.getPHOTO_ADDR(),
				R.drawable.ic_launcher, new UrlImageViewCallback() {
					@Override
					public void onLoaded(ImageView imageView,
							Bitmap loadedBitmap, String url,
							boolean loadedFromCache) {
						if (!loadedFromCache) {
						}
					}
				});

		return convertView;
	}
}